#include "updateconfwidget.h"
#include "sysconf.h"
#include "readonlydelegate.h"
#include <QMessageBox>
#include "devupdatedlg.h"

extern Channel chn[2];
extern _61850 m61850;
extern QList<DevConf> confDevList;

extern bool loadData();
extern void clearData();
extern bool createStartup(const QString &filename,QString &error);
extern bool createDatamapFile(const QString &filename,QString &error);
extern void updateChannel();

UpdateConfWidget::UpdateConfWidget(QWidget *parent) :
    QWidget(parent)
{
    setupUi(this);

    chnModel1=new ChnStandardItemModel;
    chnModel2=new ChnStandardItemModel;
    chnDevView1->setModel(chnModel1);
    chnDevView2->setModel(chnModel2);
    chnDevView1->setItemDelegateForColumn(0,new readonlyDelegate(this));
    chnDevView1->setItemDelegateForColumn(1,new readonlyDelegate(this));
    chnDevView1->setItemDelegateForColumn(2,new readonlyDelegate(this));
    chnDevView2->setItemDelegateForColumn(0,new readonlyDelegate(this));
    chnDevView2->setItemDelegateForColumn(1,new readonlyDelegate(this));
    chnDevView2->setItemDelegateForColumn(2,new readonlyDelegate(this));

    connect(chnDevView1,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(clickView1(QModelIndex)));
    connect(chnDevView2,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(clickView2(QModelIndex)));
    connect(updateBtn,SIGNAL(clicked()),this,SLOT(updateData()));
}


void UpdateConfWidget::initUI()
{

    clearData();
    loadData();


    lineEdit->setText(m61850.iedName);
    chnModel1->setDatas(chn[0].sysdev);
    chnModel2->setDatas(chn[1].sysdev);

    com1Text_2->setText(chn[0].com_name);
    com2Text_2->setText(chn[1].com_name);
    comboBox1_2->setCurrentIndex(comboBox1_2->findText(QString::number(chn[0].baud)));
    comboBox2_2->setCurrentIndex(comboBox2_2->findText(QString::number(chn[1].baud)));

}


void UpdateConfWidget:: clickView1(QModelIndex index)
{
    DevUpdateDLG dlg;
    dlg.setData(0,index.row());
    dlg.exec();
    chnModel1->setDatas(chn[0].sysdev);

}

void UpdateConfWidget:: clickView2(QModelIndex index)
{
    DevUpdateDLG dlg;
    dlg.setData(1,index.row());
    dlg.exec();
    chnModel2->setDatas(chn[1].sysdev);

}



void UpdateConfWidget::updateData()
{
    QString error;
    //更新iedname
    QString tempIEDName=lineEdit->text().trimmed();


    if(tempIEDName!=m61850.iedName)
    {
        m61850.iedName=tempIEDName;
        //更新startup.cfg
        if(!createStartup("./dist/mmslite/startup.cfg",error))
        {
            qDebug("%s",qPrintable(error));
            QMessageBox::warning(this,"提示","修改startup.cfg时出错");
        }

        //重新生成datamap.cfg
        if(!createDatamapFile("./dist/mmslite/datamap.cfg",error))
        {
            qDebug("%s",qPrintable(error));
            QMessageBox::warning(this,"提示","修改startup.cfg时出错");
        }  
    }
    updateIEDNameCid(m61850.iedName);

    chn[0].com_name=com1Text_2->text();
    chn[0].baud=comboBox1_2->currentText().toInt();

    chn[1].com_name=com2Text_2->text();
    chn[1].baud=comboBox2_2->currentText().toInt();

    //更新channel
    updateChannel();



    QMessageBox::warning(this,"提示","修改成功");
}



void UpdateConfWidget::on_chnDevView1_customContextMenuRequested(const QPoint &pos)
{
    qDebug("right clicked");
}

